package com.alibaba.citrus.cr.sales.plan.repository;

import com.alibaba.citrus.cr.sales.plan.repository.request.MessageChannelQueryRequest;
import com.alibaba.citrus.cr.sales.plan.repository.request.MessageTemplatePageQueryRequest;
import com.alibaba.citrus.cr.sales.plan.repository.request.SubWxTempInfoUserQueryRequest;
import com.alibaba.citrus.cr.sales.plan.repository.request.WxAppletMessageSendRequest;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcorder.model.dto.MessageChannelResponse;
import com.epoch.app.bcorder.model.dto.MessageTemplateResponse;
import com.epoch.app.bcorder.model.dto.QueryMessageChannelRequest;
import com.epoch.app.bcorder.model.dto.QueryMessageTemplateListRequest;
import com.epoch.app.bcorder.model.dto.QuerySubWxTempInfoUserListRequest;
import com.epoch.app.bcorder.model.dto.SendWxAppMessageRequest;
import com.epoch.app.bcorder.model.dto.WcAppletSubInfoResponse;
import com.epoch.app.bcorder.service.SalesPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author:suqing.tjf
 * @date:2021/12/6
 */
@Component
public class WxAppletRepositoryImpl implements WxAppletRepository {

    @Autowired
    private SalesPlanService salesPlanService;

    @Override
//    @RepositoryInvoker(printLog = true)
    public PageResult<List<WcAppletSubInfoResponse>> queryWcAppletSubUserList(SubWxTempInfoUserQueryRequest request) {
        QuerySubWxTempInfoUserListRequest userListRequest = new QuerySubWxTempInfoUserListRequest();
        userListRequest.setPage(request.getPage());
        userListRequest.setSize(request.getSize());
        userListRequest.setTemplateId(request.getTemplateId());
        userListRequest.setChannelId(request.getChannelId());
        Result<List<WcAppletSubInfoResponse>> result = salesPlanService.querySubWxTempInfoUserList(userListRequest);
        if (null == result || result.getTotal() < 1) {
            return null;
        }
        return PageResult.of(result.getResult(), result.getTotal());
    }

    @Override
//    @RepositoryInvoker(printLog = true)
    public Boolean sendWxAppletMessage(WxAppletMessageSendRequest request) {
        SendWxAppMessageRequest sendWxAppMessageRequest = new SendWxAppMessageRequest();
        sendWxAppMessageRequest.setBizId(request.getBizId());
        sendWxAppMessageRequest.setBizType(request.getBizType());
        sendWxAppMessageRequest.setTargetType(request.getTargetType());
        sendWxAppMessageRequest.setParams(request.getParams());
        sendWxAppMessageRequest.setReceivers(request.getReceivers());
        sendWxAppMessageRequest.setTargetId(request.getTargetId());
        sendWxAppMessageRequest.setEventCode(request.getEventCode());
        Result<Boolean> result = salesPlanService.sendWxAppMessage(sendWxAppMessageRequest);
        return null != result && null != result.getResult() && result.getResult();
    }

    @Override
//    @RepositoryInvoker(printLog = true)
    public MessageChannelResponse queryMessageChannel(MessageChannelQueryRequest request) {
        QueryMessageChannelRequest channelRequest = new QueryMessageChannelRequest();
        channelRequest.setRemoteMessageChannelId(request.getRemoteMessageChannelId());
        channelRequest.setName(request.getName());
        channelRequest.setType(request.getType());
        channelRequest.setStatus(request.getStatus());
        Result<MessageChannelResponse> result = salesPlanService.queryMessageChannel(channelRequest);
        if (null == result || null == result.getResult()) {
            return null;
        }
        return result.getResult();
    }

    @Override
//    @RepositoryInvoker(printLog = true)
    public List<MessageTemplateResponse> pageQueryMessageTemplate(MessageTemplatePageQueryRequest request) {
        QueryMessageTemplateListRequest listRequest = new QueryMessageTemplateListRequest();
        listRequest.setEventCode(request.getEventCode());
        listRequest.setStatus(request.getStatus());
        listRequest.setChannelType(request.getChannelType());
        listRequest.setChannelId(request.getChannelId());
        listRequest.setPage(request.getPage());
        listRequest.setSize(request.getSize());
        Result<List<MessageTemplateResponse>> pageResult = salesPlanService.queryMessageTemplateList(listRequest);
        if (null == pageResult || pageResult.getTotal() < 1) {
            return null;
        }
        return pageResult.getResult();
    }
}
